package com.android.kotlin.fragment

import android.arch.lifecycle.Observer
import android.support.v7.widget.StaggeredGridLayoutManager
import android.util.Log
import android.view.View
import com.android.kotlin.bean.GankBean
import com.android.kotlin.adapter.MyGrilAdapter
import com.android.kotlin.R
import com.android.kotlin.base.BaseFragment
import com.android.kotlin.base.LoadState
import com.android.kotlin.viewmodel.GrilViewModel
import com.android.kotlin.util.Resource
import kotlinx.android.synthetic.main.frgmt_gril.*

/**
 * @Description:
 * @Author: xuwenlong
 * @Email: xuwenlong @ebrun.com
 * @Version:
 * @Create: 2019/2/28 0028
 * @Modify:
 */

class GrilFragment :BaseFragment<GrilViewModel>(){


    val manager by lazy { StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL) }
    val mAdapter by lazy { MyGrilAdapter() }
    var page:Int =0

    override fun getLayoutId(): Int {
        return R.layout.frgmt_gril
    }

    override fun getLoadSirView(): Any {
        return sRefresh
    }

    override fun initView() {
        rlList.layoutManager=manager
        rlList.adapter=mAdapter
        mAdapter!!.setOnLoadMoreListener({
            mViewModel.getMore(page).observe(this@GrilFragment, Observer {
                showData(it!!, false)
            } )

        },rlList)
        sRefresh.setOnRefreshListener {
            mAdapter.setEnableLoadMore(false)//防止下拉刷新的时候，还能加载更多
            mViewModel.getRefresh().observe(this@GrilFragment, Observer {
                page=1
                showData(it!!,true)
                mAdapter.setEnableLoadMore(true)//可以出发加载更多了
            })

        }
    }

    fun showData(gankBeanResource: Resource<GankBean>, isRefresh:Boolean){
        gankBeanResource.handle(object : Resource.OnHandleCallback<GankBean>{
            override fun onSuccess(data: GankBean?) {
                page++
                if (isRefresh) {
                    mAdapter?.setNewData(gankBeanResource.data?.results!!)
                } else {
                    mAdapter?.addData(gankBeanResource.data?.results!!)
                }
                mAdapter!!.loadMoreComplete()
            }

            override fun onEmpiy() {
                mAdapter!!.loadMoreEnd()
            }

            override fun onError() {
                if(!isRefresh){
                    mAdapter!!.loadMoreFail()
                }
            }

            override fun onCompleted() {
                if(isRefresh){
                    sRefresh.finishRefresh(1000)
                }
            }
        })
    }


    override fun initData() {
        super.initData()
        dataObserver()
    }

    /**
     * 初始化加载内容
     * */
    override fun dataObserver() {
        mViewModel.getInit().observe(this@GrilFragment, Observer {
            Log.v("DaLongMain",it?.status?.name)
            mViewModel.loadState.postValue(LoadState(it?.status!!))
            showData(it!!,true)
        })
    }

    override fun setMenuVisibility(menuVisible: Boolean) {
        super.setMenuVisibility(menuVisible)
        if (this.view != null)
            this.view!!.visibility = if (menuVisible) View.VISIBLE else View.GONE
    }
}